package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.PoetryreadService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/poetryread")
public class poetryread {
    @Resource
    private PoetryreadService poetryreadService;


    /**
     * @author 牟明艳
     * @comments 保存诗词阅读任务
     * @time 2019年9月12日17:06:22
     */
    @RequestMapping("savePoteryRead.html")
    @ResponseBody
    public Map<String, Object> savePoteryRead(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname");
        }

        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author");
        }

        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }

        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj");
        }

        String pcontent = "";
        if (request.getParameter("pcontent") != null) {
            pcontent = request.getParameter("pcontent");
        }

        String fm = "";
        if (request.getParameter("fm") != null) {
            fm = request.getParameter("fm");
        }

        //音频文件
        String musicurls = "";
        if (request.getParameter("musicurls") != null) {
            musicurls = request.getParameter("musicurls");
        }

        //诗词内容文件
        String scurls = "";
        if (request.getParameter("scurls") != null) {
            scurls = request.getParameter("scurls");
        }

        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate");
        }

        String publisher = "";
        if (request.getParameter("publisher") != null) {
            publisher = request.getParameter("publisher");
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        String readid = UUID.randomUUID().toString().replaceAll("-", "");
        smap.put("readid", readid);
        smap.put("pname", pname);
        smap.put("author", author);
        smap.put("nj", nj);
        smap.put("bj", bj);
        smap.put("pcontent", pcontent);
        smap.put("fm", fm);
        smap.put("bstate", bstate);
        smap.put("publisher", publisher);
        smap.put("pubilshtime", df.format(new Date()));

        int i = poetryreadService.savePoteryRead(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            //音频文件
            if (!musicurls.equals("")) {
                List<Map<String, Object>> listmusic = (List<Map<String, Object>>) JSONArray.parse(musicurls);
                for (int k = 0; k < listmusic.size(); k++) {
                    Map<String, Object> ypmap = new HashMap<String, Object>();
                    ypmap.put("readid", readid);
                    ypmap.put("yp", listmusic.get(k).get("musicUrl"));
                    ypmap.put("mc", listmusic.get(k).get("musicmc"));
                    ypmap.put("xh", k + "");
                    poetryreadService.savePoetryreadYp(ypmap);
                }
            }

            if (!scurls.equals("")) {
                //诗词内容文件
                List<String> listsc = (List<String>) JSONArray.parse(scurls);
                for (int m = 0; m < listsc.size(); m++) {
                    String picurl = listsc.get(m);
                    String code = "poetryread";
                    String codename = "pcontent";
                    Map<String, Object> imap = new HashMap<String, Object>();
                    imap.put("picurl", picurl);
                    imap.put("code", code);
                    imap.put("codename", codename);
                    imap.put("pid", readid);
                    imap.put("xh", m + "");
                    poetryreadService.savePicture(imap);
                }
            }
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 保存诗词阅读任务
     * @time 2019年9月16日16:23:31
     */
    @RequestMapping("editPoteryRead.html")
    @ResponseBody
    public Map<String, Object> editPoteryRead(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String readid = request.getParameter("readid");

        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname");
        }

        String author = "";
        if (request.getParameter("author") != null) {
            author = request.getParameter("author");
        }

        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }

        String bj = "";
        if (request.getParameter("bj") != null) {
            bj = request.getParameter("bj");
        }

        String pcontent = "";
        if (request.getParameter("pcontent") != null) {
            pcontent = request.getParameter("pcontent");
        }

        String fm = "";
        if (request.getParameter("fm") != null) {
            fm = request.getParameter("fm");
        }

        //音频文件
        String musicurls = "";
        if (request.getParameter("musicurls") != null) {
            musicurls = request.getParameter("musicurls");
        }

        //诗词内容文件
        String scurls = "";
        if (request.getParameter("scurls") != null) {
            scurls = request.getParameter("scurls");
        }


        String bstate = "";
        if (request.getParameter("bstate") != null) {
            bstate = request.getParameter("bstate");
        }

        smap.put("readid", readid);
        smap.put("pname", pname);
        smap.put("author", author);
        smap.put("nj", nj);
        smap.put("bj", bj);
        smap.put("pcontent", pcontent);
        smap.put("fm", fm);
        smap.put("bstate", bstate);

        int i = poetryreadService.editPoteryRead(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            //删除音频文件
            poetryreadService.delPoetryreadYp(smap);
            //音频文件
            if (!musicurls.equals("")) {
                List<Map<String, Object>> listmusic = (List<Map<String, Object>>) JSONArray.parse(musicurls);
                for (int k = 0; k < listmusic.size(); k++) {
                    Map<String, Object> ypmap = new HashMap<String, Object>();
                    ypmap.put("readid", readid);
                    ypmap.put("yp", listmusic.get(k).get("yp"));
                    ypmap.put("mc", listmusic.get(k).get("mc"));
                    ypmap.put("xh", k + "");
                    poetryreadService.savePoetryreadYp(ypmap);
                }
            }

            //删除诗词内容文件
            poetryreadService.delScPicture(smap);
            if (!scurls.equals("")) {
                //诗词内容文件
                List<String> listsc = (List<String>) JSONArray.parse(scurls);
                for (int m = 0; m < listsc.size(); m++) {
                    String picurl = listsc.get(m);
                    String code = "poetryread";
                    String codename = "pcontent";
                    Map<String, Object> imap = new HashMap<String, Object>();
                    imap.put("picurl", picurl);
                    imap.put("code", code);
                    imap.put("codename", codename);
                    imap.put("pid", readid);
                    imap.put("xh", m + "");
                    poetryreadService.savePicture(imap);
                }
            }

            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 获取我的任务
     * @time 2019年9月12日20:11:53
     */
    @RequestMapping("getPoetryread.html")
    @ResponseBody
    public Map<String, Object> getPoetryread(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> setmap = new HashMap<String, Object>();
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }

        String keyword = "";
        if (request.getParameter("keyword") != null) {
            keyword = request.getParameter("keyword");
        }

        String user_type = request.getParameter("user_type");//1学生 2教师
        if (user_type.equals("1")) {
            String nj = "";
            if (request.getParameter("nj") != null) {
                nj = request.getParameter("nj");
            }
            String bj = "";
            if (request.getParameter("bj") != null) {
                bj = request.getParameter("bj");
            }
            setmap.put("nj", nj);
            setmap.put("bj", bj);
            setmap.put("usid", usid);
            setmap.put("keyword", keyword);
        } else {
            setmap.put("publisher", usid);
            setmap.put("keyword", keyword);
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        setmap.put("start", startn);
        setmap.put("end", endn);

        List<Map<String, Object>> list1 = poetryreadService.getPoetryread(setmap);
        int totalnum = poetryreadService.countPoetryread(setmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 每天随机分配十个人的作业
     * @time 2019年9月13日16:23:56
     */
    @RequestMapping("getHomeWork.html")
    @ResponseBody
    public List<Map<String, Object>> getHomeWork(HttpServletRequest request, HttpServletResponse response) {
        String dlzh = request.getParameter("dlzh");
        //查询当前星期几
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);

        //每天随机分配十个人的作业，星期五查询所有的作业
        Map<String, Object> setmap = new HashMap<String, Object>();
        setmap.put("dlzh", dlzh);
        setmap.put("weekday", weekday - 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String riqi = df.format(new Date());
        setmap.put("riqi", riqi);

        int page = weekday - 1;
        if ((page == 1) || (page == 2) || (page == 3) || (page == 4)) {//如果为星期五
            setmap.put("start", 0);
            setmap.put("end", 10);
        } else {
            setmap.put("start", 0);
            setmap.put("end", 1000);
        }
        List<Map<String, Object>> list1 = poetryreadService.getHomeWork(setmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 分页查询作业检查任务设置
     * @time 2019年9月14日17:02:44
     */

    @RequestMapping("search_poetryread.html")
    @ResponseBody
    public Map<String, Object> search_poetryread(HttpServletRequest request, HttpServletResponse response) {
        String pname = "";
        if (request.getParameter("pname") != null) {
            pname = request.getParameter("pname").toString().trim();
        }

        String riqi = "";
        if (request.getParameter("riqi") != null) {
            riqi = request.getParameter("riqi").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pname", pname);
        gmap.put("riqi", riqi);

        List<Map<String, Object>> list1 = poetryreadService.search_poetryread(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 删除作业检查任务设置
     * @time 2019年9月14日17:26:13
     */
    @RequestMapping("del_poetryreadscore.html")
    @ResponseBody
    public Map<String, Object> del_poetryreadscore(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }
        smap.put("readid", readid);

        String riqi = "";
        if (request.getParameter("riqi") != null) {
            riqi = request.getParameter("riqi");
        }
        smap.put("riqi", riqi);

        Map<String, Object> remap = new HashMap<String, Object>();
        int i = poetryreadService.del_poetryreadscore(smap);
        if (i <= 0) {
            remap.put("msg", "删除失败!");
            remap.put("result", false);
            return remap;
        }
        remap.put("msg", "删除成功!");
        remap.put("result", true);
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 分页查询作业检查任务设置
     * @time 2019年9月14日17:02:44
     */

    @RequestMapping("getAllPoetryRead.html")
    @ResponseBody
    public List<Map<String, Object>> getAllPoetryRead(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list1 = poetryreadService.getAllPoetryRead();
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 添加诗词阅读
     * @time 2019年9月15日21:18:52
     */
    @RequestMapping("add_poetryreadscore.html")
    @ResponseBody
    public Map<String, Object> add_poetryreadscore(HttpServletRequest request, HttpServletResponse response) {
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }

        String reads = request.getParameter("reads");
        List<Map<String, Object>> listmap = new ArrayList<>();
        List<Map<String, Object>> listr = (List<Map<String, Object>>) JSONArray.parse(reads);
        for (int i = 0; i < listr.size(); i++) {
            Map<String, Object> maps = listr.get(i);
            String riqi = maps.get("riqi").toString();
            String weekday = maps.get("weekday").toString();
            String zhstr = maps.get("zh").toString();
            List<Map<String, Object>> liszh = (List<Map<String, Object>>) JSONArray.parse(zhstr);
            for (int j = 0; j < liszh.size(); j++) {
                String dlzh = liszh.get(j).get("dlzh").toString();
                if (!dlzh.equals("")) {
                    Map<String, Object> zhmap = new HashMap<>();
                    zhmap.put("riqi", riqi);
                    zhmap.put("weekday", weekday);
                    zhmap.put("dlzh", dlzh);
                    zhmap.put("readid", readid);
                    listmap.add(zhmap);
                }
            }
        }
        Map<String, Object> remap = new HashMap<String, Object>();
        if (listmap.size() <= 0) {
            remap.put("msg", "提交失败，没有任何账号!");
            remap.put("result", false);
            return remap;
        }

        for (int i = 0; i < listmap.size(); i++) {
            Map<String, Object> maps = listmap.get(i);
            int m = poetryreadService.add_poetryreadscore(maps);
            if (m <= 0) {
                remap.put("msg", "保存失败!");
                remap.put("result", false);
                return remap;
            }
        }
        remap.put("msg", "保存成功!");
        remap.put("result", true);
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 修改诗词阅读
     * @time 2019年9月16日10:53:50
     */
    @RequestMapping("edit_poetryreadscore.html")
    @ResponseBody
    public Map<String, Object> edit_poetryreadscore(HttpServletRequest request, HttpServletResponse response) {
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }

        String riqi = "";
        if (request.getParameter("riqi") != null) {
            riqi = request.getParameter("riqi");
        }

        String weekday = "";
        if (request.getParameter("weekday") != null) {
            weekday = request.getParameter("weekday");
        }

        String zh = "";
        if (request.getParameter("zh") != null) {
            zh = request.getParameter("zh");
        }


        List<Map<String, Object>> listmap = new ArrayList<>();
        List<Map<String, Object>> liszh = (List<Map<String, Object>>) JSONArray.parse(zh);

        for (int j = 0; j < liszh.size(); j++) {
            String dlzh = liszh.get(j).get("dlzh").toString();
            if (!dlzh.equals("")) {
                Map<String, Object> zhmap = new HashMap<>();
                zhmap.put("riqi", riqi);
                zhmap.put("weekday", weekday);
                zhmap.put("dlzh", dlzh);
                zhmap.put("readid", readid);
                listmap.add(zhmap);
            }
        }


        Map<String, Object> remap = new HashMap<String, Object>();
        if (listmap.size() <= 0) {
            remap.put("msg", "提交失败，没有任何账号!");
            remap.put("result", false);
            return remap;
        }

        Map<String, Object> setmap = new HashMap<>();
        setmap.put("readid", readid);
        setmap.put("riqi", riqi);
        poetryreadService.del_poetryreadscore(setmap);

        for (int i = 0; i < listmap.size(); i++) {
            Map<String, Object> maps = listmap.get(i);
            int m = poetryreadService.edit_poetryreadscore(maps);
            if (m <= 0) {
                remap.put("msg", "修改失败!");
                remap.put("result", false);
                return remap;
            }
        }
        remap.put("msg", "修改成功!");
        remap.put("result", true);
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 分页查询作业检查任务设置
     * @time 2019年9月14日17:02:44
     */

    @RequestMapping("getPoetryReadById.html")
    @ResponseBody
    public Map<String, Object> getPoetryReadById(HttpServletRequest request, HttpServletResponse response) {
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }
        Map<String, Object> setmap = new HashMap<>();
        setmap.put("readid", readid);
        Map<String, Object> maps = poetryreadService.getPoetryReadById(setmap);
        return maps;
    }


    /**
     * @author 牟明艳
     * @comments 根据诗词ID查询作业情况
     * @time 2019年9月16日20:27:41
     */
    @RequestMapping("getAllHomeworkByreadid.html")
    @ResponseBody
    public Map<String, Object> getAllHomeworkByreadid(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> setmap = new HashMap<String, Object>();
        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束

        setmap.put("usid", usid);
        setmap.put("readid", readid);
        setmap.put("start", startn);
        setmap.put("end", endn);

        List<Map<String, Object>> list1 = poetryreadService.getAllHomeworkByreadid(setmap);
        int totalnum = poetryreadService.countAllHomeworkByreadid(setmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 根据诗词ID查询作业情况积分排名
     * @time 2019年9月16日20:27:41
     */
    @RequestMapping("getHomeworkPmByreadid.html")
    @ResponseBody
    public Map<String, Object> getHomeworkPmByreadid(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> setmap = new HashMap<String, Object>();

        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }

        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }

        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束

        setmap.put("readid", readid);
        setmap.put("usid", usid);
        setmap.put("start", startn);
        setmap.put("end", endn);

        List<Map<String, Object>> list1 = poetryreadService.getHomeworkPmByreadid(setmap);
        int totalnum = poetryreadService.countHomeworkPmByreadid(setmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 保存诗词阅读任务
     * @time 2019年9月12日17:06:22
     */
    @RequestMapping("savePoteryReadLog.html")
    @ResponseBody
    public Map<String, Object> savePoteryReadLog(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }

        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }

        String username = "";
        if (request.getParameter("username") != null) {
            username = request.getParameter("username");
        }

        String yp = "";
        if (request.getParameter("yp") != null) {
            yp = request.getParameter("yp");
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("readid", readid);
        smap.put("usid", usid);
        smap.put("username", username);
        smap.put("yp", yp);
        smap.put("pubilshtime", df.format(new Date()));
        smap.put("pstatr", "0");

        int i = poetryreadService.savePoteryReadLog(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 保存诗词阅读任务打分
     * @time 2019年9月19日15:41:42
     */
    @RequestMapping("setPoetryreadlogStar.html")
    @ResponseBody
    public Map<String, Object> setPoetryreadlogStar(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String readid = "";
        if (request.getParameter("readid") != null) {
            readid = request.getParameter("readid");
        }

        String pstatr = "";
        if (request.getParameter("pstatr") != null) {
            pstatr = request.getParameter("pstatr");
        }

        String pusid = "";
        if (request.getParameter("pusid") != null) {
            pusid = request.getParameter("pusid");
        }

        String pusername = "";
        if (request.getParameter("pusername") != null) {
            pusername = request.getParameter("pusername");
        }

        String usid = "";
        if (request.getParameter("usid") != null) {
            usid = request.getParameter("usid");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        smap.put("readid", readid);
        smap.put("pstatr", pstatr);
        smap.put("pusid", pusid);
        smap.put("pusername", pusername);
        smap.put("pltime", df.format(new Date()));
        smap.put("usid", usid);
        int i = poetryreadService.setPoetryreadlogStar(smap);

        Map<String, Object> remap = new HashMap<String, Object>();
        if (i > 0) {
            remap.put("msg", "保存成功!");
            remap.put("result", true);
        } else {
            remap.put("msg", "保存失败!");
            remap.put("result", false);
        }
        return remap;
    }


}
